package com.example.dbcms.service.impl;


import com.example.dbcms.domain.Parking;
import com.example.dbcms.mapper.ParkingDao;
import com.example.dbcms.mapper.ResidentDao;
import com.example.dbcms.service.ParkingService;
import com.example.dbcms.utils.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class ParkingServiceImpl implements ParkingService {

    @Autowired
    private ParkingDao parkingDao;

    @Autowired
    private ResidentDao residentDao;

    @Override
    public PageInfo<Parking> selectAllParking(Parking parking) {
        PageHelper.startPage(parking.getPageNum(),10);
        PageInfo<Parking> parkingPageInfo = new PageInfo<>(parkingDao.selectAllParking(parking));
        return parkingPageInfo;
    }

    @Override
    public List<Parking> selectAllParkingByCondition(Map<String, String> mapCondition) {
        return null;
    }

    @Override
    public Parking getParkingInfo(int id) {
        return parkingDao.getParkingInfo(id);
    }

    @Override
    public int updateParkingInfo(Parking parking) {
        return parkingDao.updateParkingInfo(parking);
    }

    @Override
    public int updateParkingInfoByUser(Parking parking) {
        return parkingDao.updateParkingInfoByUser(parking);
    }

    @Override
    public int deleteParkingInfo(int id) {
        return parkingDao.deleteParkingInfo(id);
    }

    @Override
    public int deleteMultipleParkingInfo(List<Integer> ids) {
        return parkingDao.deleteMultipleParkingInfo(ids);
    }

    @Override
    public int addParking(Parking parking) {
        if(!DateUtil.isValidDate(parking.getBuyDate())){
            parking.setBuyDate(DateUtil.getCurrentDate());
        }
        parking.setParkingId(parkingDao.getMaxParkingId()+1);
        parking.setResidentId(residentDao.getResidentId(parking.getBuildingNo(),parking.getRoomNo()));
        return parkingDao.addParking(parking);
    }
}

